import 'package:dio/dio.dart';

// 删除消息 by messageId
Future<Response<dynamic>> deleteMessageByMessageId({
  required int messageId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.delete('/message/$messageId');
  return response;
}
Future<Response<dynamic>> getLatestMessagesBySenderId({
  required int userId,
  required Dio dio,
}) async {
  try {
    Response<dynamic> response = await dio.get('/message/latest/$userId');
    return response;
  } catch (e) {
    print('Error fetching latest messages: $e');
    throw Exception('Failed to fetch latest messages: $e');
  }
}
// 获取用户之间的消息列表
Future<Response<dynamic>> getMessageByUserIdAndReceiverId({
  required int userId,
  required int receiverId,
  required Dio dio,
}) async {
  Response<dynamic> response = await dio.get('/message/$userId/$receiverId');
  return response;
}

Future<Response<dynamic>> sendMessageByUserIdAndReceiverId({
  required String messageText,
  required int senderId,
  required int receiverId,
  required Dio dio,
}) async {
  try {
    // Adjusted URL to include senderId and receiverId
    Response<dynamic> response = await dio.post(
      '/message/$senderId/$receiverId',
      data: {
        'messageText': messageText,
      },
      options: Options(
        headers: {
          'Content-Type': 'application/json', // Adjust to the appropriate content type
        },
      ),
    );
    return response;
  } catch (e) {
    if (e is DioError) {
      print('DioError: ${e.response?.data}');
    } else {
      print('Error: $e');
    }
    throw Exception('发送消息失败: $e');
  }
}

